package com.lwl.personal_practice;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.widget.TextView;

import com.lwl.personal_practice.util.ColorUtil;

public class MainActivity extends AppCompatActivity {

    private AlertDialog customSpinnerDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EdgeToEdge.enable(this);
//        Button mButton = findViewById(R.id.shop_button);
//        mButton.setBackgroundColor(Color.parseColor("#FF3D00"));;
//        mButton.setOnClickListener(new MyOnClickListener());

        Button mButton1 = findViewById(R.id.button1);
//        mButton.setBackgroundColor(Color.parseColor("#F44336"));
//        mButton.setTextColor(Color.WHITE);
        mButton1.setOnClickListener(new MyOnClickListener());
        Button mButton2 = findViewById(R.id.button2);
        mButton2.setOnClickListener(new MyOnClickListener());
    }
    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.button1) {
                initSpinners();
            }else if (v.getId() == R.id.shop_button) {
                TextView tv1 = findViewById(R.id.tv_car_num);
                Integer num = Integer.parseInt(tv1.getText().toString());
                tv1.setText(String.valueOf(num + 1));
            } else if (v.getId() == R.id.button2) {
                Button mButton2 = findViewById(R.id.button2);
                Drawable originalDrawable = getResources().getDrawable(R.drawable.btn_orange);

                if (originalDrawable instanceof GradientDrawable) {
                    GradientDrawable shapeDrawable = (GradientDrawable) originalDrawable;

                    // 创建一个新的 GradientDrawable 实例
                    GradientDrawable newShapeDrawable = new GradientDrawable();

                    // 复制所有属性，除了 solid 部分
                    newShapeDrawable.setCornerRadius(shapeDrawable.getCornerRadius());

                    // 设置新的 solid 颜色
                    newShapeDrawable.setColor(Color.parseColor(ColorUtil.getRanDomColor())); // 这里我们将颜色改为红色

                    // 将修改后的 drawable 设置回按钮
                    mButton2.setBackground(newShapeDrawable);
                } else {
                    throw new IllegalArgumentException("Original drawable is not a GradientDrawable.");
                };
            }
        }
    }
    public void initSpinners(){
        if (customSpinnerDialog == null || !customSpinnerDialog.isShowing()) {
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.custom_spinner_dialog, null);
            Spinner spinner = dialogView.findViewById(R.id.my_spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.options, R.layout.spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);

            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialogTheme);
            builder.setView(dialogView);
            customSpinnerDialog = builder.create(); // 在这里创建并保存对话框实例

            customSpinnerDialog.setOnShowListener(dialogInterface -> {
                WindowManager windowManager = getWindowManager();
                Display display = windowManager.getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int screenWidth = size.x;
                int dialogWidth = screenWidth * 3 / 4; // Set the width to one third of the screen width

                customSpinnerDialog.getWindow().setLayout(dialogWidth, WindowManager.LayoutParams.WRAP_CONTENT);
            });

            customSpinnerDialog.show(); // 显示对话框
        } else {
            if (customSpinnerDialog.isShowing()) {
                customSpinnerDialog.dismiss(); // 如果对话框已显示，则先关闭再重新显示
            }
            customSpinnerDialog = new AlertDialog.Builder(this, R.style.CustomAlertDialogTheme).setView(R.layout.custom_spinner_dialog).create();
            customSpinnerDialog.show();
        }
    }
}